package leetcode.code0814;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	public TreeNode pruneTree(TreeNode root) {
		return (TreeNode) this.check(root)[1];
	}

	private Object[] check(TreeNode root) {
		if (root == null) {
			return new Object[] { 0, null };
		}
		if (root.left == null && root.right == null) {
			return root.val == 0 ? new Object[] { 0, null } : new Object[] { 1, root };
		}
		Object[] b1 = this.check(root.left);
		Object[] b2 = this.check(root.right);
		root.left = (int) b1[0] == 0 ? null : (TreeNode) b1[1];
		root.right = (int) b2[0] == 0 ? null : (TreeNode) b2[1];
		if ((int) b1[0] == 0 && (int) b2[0] == 0 && root.val == 0) {
			return new Object[] { 0, null };
		}
		return new Object[] { 1, root };
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
